package org.crag.cafca.gui.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.SortedSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.crag.cafca.db.SqlConnector;
import org.crag.cafca.logic.time.Event;
import org.crag.cafca.logic.time.FamilyEvent;
import org.crag.cafca.logic.time.SourceEvent;
import org.crag.cafca.logic.time.SourceManagementEvent;
import org.crag.cafca.logic.time.TimeLine;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONObject;
/**
 * Used to see one particular TimeEvent of the user, based on the date he has chosen. 
 *
 */
@WebServlet("/SeeSpecificEventServlet")
public class SeeSpecificTimeEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
    public SeeSpecificTimeEventServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = (HttpSession) request.getSession(); 
		TimeLine timeline = (TimeLine) session.getAttribute("timeline"); 
		String date = request.getParameter("date"); 
		JSONObject resp = createJson(timeline,getDateTimeFromString(date)); 
		response.getWriter().print(resp.toString());
		response.setStatus(HttpServletResponse.SC_OK);
	}
	
	private JSONObject createJson(TimeLine timeline,DateTime date)
	{
		JSONObject toSendBack = new JSONObject(); 
		JSONArray dataArray = new JSONArray(); 
		SortedSet<Event> events = timeline.getEvents(); 
		
		for(Event event : events)
		{
			if (event.getStartDateTime().equals(date))
			{
				JSONObject row = new JSONObject(); 
				row.put("description", event.getDescription()); 
				row.put("startDate", event.getStartDateTime().toString().substring(0, 10)); 
				row.put("endDate", event.getEndDateTime().toString().substring(0, 10)); 
				row.put("tags", event.getTagString());
				row.put("value", event.getValue());							
			
				if (event instanceof SourceEvent)
				{
					SourceEvent sourceEvent=(SourceEvent)event;
					row.put("name", sourceEvent.getSourceId()); 
				}
	
				if (event instanceof SourceManagementEvent)
				{
					SourceManagementEvent sourceManagementEvent=(SourceManagementEvent)event;
					row.put("name", sourceManagementEvent.getSourceId()); 
				}
	
				if (event instanceof FamilyEvent)
				{
					FamilyEvent familyEvent=(FamilyEvent)event;
				}
				dataArray.put(row); 
			}
		}
		
		toSendBack.put("data", dataArray); 
		toSendBack.put("success", true); 
		
		return toSendBack; 
	}
	
	/*Convert a date into MySQL accepted format*/
	private DateTime getDateTimeFromString(String date)
	{
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd"); 
		DateTime dt = formatter.parseDateTime(date); 
		return dt; 	
	}
}
